﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Noebe.MemoryCaches.Core
{
    public class CacheNoebeConfiguration
    {
        CacheSetting defaultSetting = new CacheSetting();

        List<CacheSetting> settings = new List<CacheSetting>();

        public CacheSetting Add(string settingName)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            return Add(settingName, -1);
        }
        public CacheSetting Add(string settingName, int timeSpan)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            return Add(settingName, timeSpan, StringHelper.default_cache_maxcapacity);
        }
        public CacheSetting Add(string settingName, int timeSpan, int capacity)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            return Add(settingName, timeSpan, capacity, CacheMode.ShallowCache);
        }
        public CacheSetting Add(string settingName, int timeSpan, int capacity, CacheMode mode)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            CacheSetting setting = new CacheSetting();

            setting.SettingName = settingName.Trim().ToUpper();
            setting.CacheMode = mode;
            setting.MaxCapacity = capacity;
            setting.TimeSpan = timeSpan;

            settings.Add(setting);

            return setting;

        }

        /// <summary>
        /// 有就返回 没有就新建一个再返回
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public CacheSetting Get(string name)
        {
            if (string.IsNullOrEmpty(name))
                return defaultSetting;

            name = name.Trim().ToUpper();

            foreach (CacheSetting setting in settings)
            {
                if (setting.SettingName.Equals(name, StringComparison.OrdinalIgnoreCase))
                    return setting;
            }

            CacheSetting nsetting = new CacheSetting(name);

            settings.Add(nsetting);

            return nsetting;
        }

        public void Clear()
        {
            settings.Clear();
        }

        public void Verify()
        {
            if (DefaultSetting == null)
                DefaultSetting = new CacheSetting();

            if (!string.IsNullOrEmpty(DefaultSetting.SettingName))
                DefaultSetting.SettingName = DefaultSetting.SettingName.Trim().ToUpper();

            foreach (CacheSetting setting in settings)
            {
                if (!string.IsNullOrEmpty(setting.SettingName))
                    setting.SettingName = setting.SettingName.Trim().ToUpper();
            }
        }

        public List<CacheSetting> Settings
        {
            get
            {
                return settings;
            }
            set { settings = value; }
        }

        public CacheSetting DefaultSetting
        {
            get
            {
                return defaultSetting;
            }
            set { defaultSetting = value; }
        }
    }
}
